package cz.cvut.fel.riak.parse;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.io.LineReaderInputStream;
import org.apache.james.mime4j.io.LineReaderInputStreamAdaptor;
import org.apache.james.mime4j.parser.Field;
import org.apache.james.mime4j.parser.MimeEntityConfig;
import org.apache.james.mime4j.parser.MimeTokenStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.cvut.fel.riak.email.Email;

public class Mime4JParserImpl implements Parser {
	
	final static Logger log = LoggerFactory.getLogger(Mime4JParserImpl.class);
	
	/* (non-Javadoc)
	 * @see cz.cvut.fel.riak.parse.Parser#parseEmail(java.lang.String)
	 */
	public Email parseEmail(String path) {
		/*
		MimeEntityConfig config = new MimeEntityConfig();
		config.setMaxContentLen(Long.MAX_VALUE);
		config.setMaxLineLen(Integer.MAX_VALUE);*/
		
		MimeTokenStream parser = new MimeTokenStream();
		
		Email email = new Email();
		boolean endHeaders = false;
		
		
		try {
			FileInputStream fis = new FileInputStream(path);
			log.info("Exists:" + (new File(path).exists()));
			CountingInputStream cis = new CountingInputStream(fis);
			//LineReaderInputStreamAdaptor lreader = new LineReaderInputStreamAdaptor(new BufferedInputStream(fis),Integer.MAX_VALUE);
			
			parser.parse(cis);
			int parts = 0;
			for (int state = parser.getState(); state != MimeTokenStream.T_END_OF_STREAM; state = parser.next()) {
				switch (state) {
				case MimeTokenStream.T_BODY:
					endHeaders = true;
					parser.getInputStream().toString();
					LineReaderInputStreamAdaptor adaptor = (LineReaderInputStreamAdaptor) parser.getInputStream();
					String body = ""+adaptor+"";
					System.out.println( parser.getBodyDescriptor());
					System.out.println( "DESCRIPTOR: ["+parser.getBodyDescriptor()+"]");

					System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BODY <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
					
					break;
				case MimeTokenStream.T_FIELD:
					if (!endHeaders) {
						Field f = parser.getField();
						email.addHeader(f.getName(), f.getBody());
					}
				
					break;
				case MimeTokenStream.T_START_MULTIPART:
					parts++;
					System.out.println( cis.getByteCount());
					//System.out.println(parser.getInputStream());
					log.info(parts+" Multipart message detexted,"
							+ " header data = " + parser.getBodyDescriptor().getBoundary()+" file "+path);

				}
			}
			
			
			return email;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}
